/**
 * 获取相册拍摄图片
 */
export function chooseImage(count = 1, sourceType = ['album', 'camera']) {
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			count: count,
			sizeType: ['original', 'compressed'],
			sourceType,
			success: (res) => resolve(res.tempFilePaths),
			fail: (err) => reject(err)
		});
	})
}

export function chooseVideo(count = 1) {
	return new Promise((resolve, reject) => {
		uni.chooseVideo({
			count: count,
			sizeType: ['original'],
			success: (res) => {
				resolve(res.tempFilePath)
			},
			fail: (err) => {
				reject(err)
			}
		});
	})
}

export function chooseFile(count = 1) {
	return new Promise((resolve, reject) => {
		uni.chooseFile({
			count: count,
			sizeType: ['original'],
			sourceType: ['album', 'camera'],
			success: (res) => resolve(res.tempFilePath),
			fail: () => reject()
		});
	})
}

export function phoneFormat(phone) {
	if (!regex.phone.test(phone)) return phone
	return phone.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2")
}

/**
 * 返回事件
 */
export function openerEvent(_this, entity) {
	// #ifdef MP-WEIXIN
	const eventChannel = _this.getOpenerEventChannel()
	// #endif
	// #ifdef H5
	const eventChannel = _this.getOpenerEventChannel()
	// #endif
	// #ifdef APP-PLUS
	const eventChannel = _this.$scope.eventChannel
	// #endif
	if (entity == 'onLoad') return eventChannel
	if (eventChannel && eventChannel.emit) {
		eventChannel.emit('backEvent', entity || null);
	}
}


export function removeStorages() {
	uni.removeStorageSync('userInfo')
	uni.removeStorageSync('Token')
}

export function windowSearch(search) {
	return JSON.parse('{"' + search.split("?").join("").replace(/=/g, '":"').replace(/&/g, '","') + '"}');
}

export function listFormat(arr = [], pageSize = 1) {
	let newArr = [],
		tmpArr = [];
	arr.forEach((item, index) => (tmpArr.push(item), (index % pageSize === pageSize - 1 || index === arr.length -
		1 ? (newArr.push(tmpArr), tmpArr = []) : null)));
	return newArr;
}

/**
 * 随机字符串
 */
export function randomEvent(quantity, amount) {
	let chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
		'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
	]
	let arr = []
	for (let k = 0; k < amount; k++) {
		let str = ''
		for (let i = 0; i < quantity; i++) {
			let id = Math.ceil(Math.random() * 35)
			str += chars[id]
		}
		arr.push(str)
	}
	return arr.join('-')
}

export function getPrice(value, type = 1) {
	const [price, dot] = (value - 0).toFixed(2).split('.')
	if (type == 1) return price
	return dot ? `.${dot}` : '.00'
}

export function complete() {
	uni.hideLoading()
	uni.stopPullDownRefresh()
}

/**
 * 保存图片到相册
 */
export function saveImageToPhotosAlbum(tempFilePath) {
	return new Promise((resolve, reject) => {
		uni.saveImageToPhotosAlbum({
			filePath: tempFilePath,
			success: () => showSuccess('保存成功'),
			fail: (err) => reject(err)
		})
	})
}

/**
 * 下载文件 获取临时文件路径
 */
export function downloadFile(url) {
	return new Promise((resolve, reject) => {
		uni.downloadFile({
			url,
			success: (res) => resolve(res.tempFilePath),
			fail: (err) => reject(err)
		})
	})
}
/**
 * 获取客户端唯一的推送标识
 */
export function getPushClientId() {
	return new Promise((resolve, reject) => {
		uni.getPushClientId({
			success: (res) => resolve(res),
			fail: (err) => {
				console.log(err);
				reject(err)
			}
		})
	})
}

export function selectorQuery(_this, id) {
	return new Promise((resolve, reject) => {
		const query = uni.createSelectorQuery().in(_this);
		query.select(`#${id}`).boundingClientRect(data => resolve(data)).exec();
	})
}

/**
 * 格式化富文本内容
 */
export function formatEditor(str) {
	const html = str
	let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
		match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
		match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
		match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
		return match;
	});
	newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
		match = match.replace(/width:[^;]+;/gi, 'width:100%;').replace(/width:[^;]+;/gi, 'width:100%;');
		return match;
	});
	newContent = newContent.replace(/<br[^>]*\/>/gi, '');
	newContent = newContent.replace(/\<img/gi,
		'<img style="width:100%;display:inline-block;"');
	return newContent;
}